package com.starbaba.core.web.tree;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;

import com.starbaba.core.utils.config.ConfigFileFactory;
import com.starbaba.core.web.constant.Constants;
import com.starbaba.system.menu.model.MenuVO;

/**
 * 构建菜单树--(只支持二级菜单树) for 万众生活管家项目
 * 
 * @author Administrator
 *
 */
@SuppressWarnings("all")
public class MenuTreeBuilder {
	public static Boolean isLRlayout;

	static {
		// 或者布局方式{true : 左右布局,左边菜单，flase：横向菜单}
		String isLRlayoutValue = ConfigFileFactory.getInstance().get("application").getValue("web.layout.islf", "true");
		isLRlayout = Boolean.valueOf(isLRlayoutValue);
	}

	/**
	 * 
	 * @param topMenuList
	 *            一级菜单列表
	 * @param rootMenuSubsMap
	 *            一级菜单MENUID&二级菜单MAP
	 * @param contentPath
	 *            服务上下文
	 * @return
	 */
	/*
	 * public static String buildTree(List<MenuVO> topMenuList
	 * ,TreeMap<String,List<MenuVO>> rootMenuSubsMap,String contentPath){
	 * if(topMenuList == null || topMenuList.size() == 0){ return ""; }
	 * StringBuffer html = new StringBuffer(); String liclass =(isLRlayout ==
	 * true )? "" :"hover";
	 * 
	 * for(MenuVO topMenu : topMenuList){ String topMenuName =
	 * topMenu.getLname(); String topMenuId = topMenu.getControlid(); String
	 * topMenuIconClass = topMenu.getIconclass();
	 * if(StringUtils.isBlank(topMenuIconClass)){ topMenuIconClass =
	 * "menu-icon fa fa-list"; } boolean hasChild =
	 * hasChild(topMenuId,rootMenuSubsMap);
	 * 
	 * if(hasChild){ html.append("<li class='").append(liclass).append("'>");
	 * html.append("	<a class='dropdown-toggle'>");
	 * html.append("       <i class='"
	 * ).append(topMenuIconClass).append("'></i>");//图标
	 * html.append("		<span class='menu-text'>"
	 * ).append(topMenuName).append("</span>");
	 * html.append("   	<b class='arrow fa fa-angle-down'></b>");
	 * html.append("   </a>"); html.append("   <b class='arrow'></b>");
	 * html.append("	<ul class='submenu'>");
	 * 
	 * List<MenuVO> childMenuList = rootMenuSubsMap.get(topMenuId); for(MenuVO
	 * childMenu : childMenuList){ String menuname = childMenu.getLname();
	 * String url = "javascript:void(0)";
	 * if(StringUtils.isNotBlank(childMenu.getUrl())){ url = contentPath +
	 * childMenu.getUrl(); }
	 * html.append("<li class='").append(liclass).append("'>");
	 * html.append("<a class='ioscut' target='main_frame' href='"
	 * ).append(url).append("'>");
	 * html.append("       <i class='menu-icon fa fa-caret-right'></i>");//图标
	 * html
	 * .append("      <span class='menu-text'>").append(menuname).append("</span>"
	 * ); html.append("</a><b class='arrow fa fa-angle-down'></b></li>"); }
	 * html.append("   </ul>"); html.append("</li>"); }else{
	 * html.append("<li class='").append(liclass).append("'>");
	 * html.append("<a target='main_frame' href='javascript:void(0)'>");
	 * html.append("<i class='menu-icon fa fa-tachometer'></i>");//图标
	 * html.append
	 * ("	<span class='menu-text'>").append(topMenuName).append("</span>");
	 * html.append("</a><b class='arrow'></b>"); html.append("</li>"); } }
	 * return html.toString(); }
	 */

	public static String buildTree(List<MenuVO> topMenuList, TreeMap<String, List<MenuVO>> rootMenuSubsMap,
			TreeMap<String, List<MenuVO>> rootChildMenuSubsMap, String contentPath) {
		if (topMenuList == null || topMenuList.size() == 0) {
			return "";
		}
		StringBuffer html = new StringBuffer();
		String liclass = (isLRlayout == true) ? "" : "hover";

		for (MenuVO topMenu : topMenuList) {
			String topMenuName = topMenu.getLname();
			String topMenuId = topMenu.getControlid();
			String topMenuIconClass = topMenu.getIconclass();
			if (StringUtils.isBlank(topMenuIconClass)) {
				topMenuIconClass = "menu-icon fa fa-list";
			}
			boolean hasChild = hasChild(topMenuId, rootMenuSubsMap);

			if (hasChild) {
				html.append("<li class='").append(liclass).append("'>");
				html.append("	<a class='dropdown-toggle'>");
				html.append("       <i class='").append(topMenuIconClass).append("'></i>");// 图标
				html.append("		<span class='menu-text'>").append(topMenuName).append("</span>");
				html.append("   	<b class='arrow fa fa-angle-down'></b>");
				html.append("   </a>");
				html.append("   <b class='arrow'></b>");
				html.append("	<ul class='submenu'>");

				List<MenuVO> _childMenuList = rootMenuSubsMap.get(topMenuId);
				for (MenuVO _childMenu : _childMenuList) {
					String _url = "javascript:void(0)";
					if (StringUtils.isNotBlank(_childMenu.getUrl())) {
						_url = contentPath + _childMenu.getUrl();
					}
					String _childMenuName = _childMenu.getLname();
					String _childMenuId = _childMenu.getControlid();
					String _childMenuIconClass = _childMenu.getIconclass();
					if (StringUtils.isBlank(_childMenuIconClass)) {
						_childMenuIconClass = "menu-icon fa fa-list";
					}

					boolean _hasChild = hasChild(_childMenuId, rootChildMenuSubsMap);
					if (_hasChild) {
						html.append("<li class='").append(liclass).append("'>");
						html.append("	<a class='dropdown-toggle'>");
						html.append("       <i class='").append(_childMenuIconClass).append("'></i>");// 图标
						html.append("		<span class='menu-text'>").append(_childMenuName).append("</span>");
						html.append("   	<b class='arrow fa fa-angle-down'></b>");
						html.append("   </a>");
						html.append("   <b class='arrow'></b>");
						html.append("	<ul class='submenu'>");

						List<MenuVO> __childMenuList = rootChildMenuSubsMap.get(_childMenuId);
						for (MenuVO __childMenu : __childMenuList) {
							String __childMenuName = __childMenu.getLname();
							String __url = "javascript:void(0)";
							if (StringUtils.isNotBlank(__childMenu.getUrl())) {
								__url = contentPath + __childMenu.getUrl();
							}
							html.append("<li class='").append(liclass).append("'>");
							html.append("<a class='ioscut' target='main_frame' href='").append(__url).append("'>");
							html.append("       <i class='menu-icon fa fa-caret-right'></i>");// 图标
							html.append("      <span class='menu-text'>").append(__childMenuName).append("</span>");
							html.append("</a><b class='arrow fa fa-angle-down'></b></li>");
						}
						html.append("   </ul>");
						html.append("</li>");
					} else {
						html.append("<li class='").append(liclass).append("'>");
						html.append("<a class='ioscut' target='main_frame' href='").append(_url).append("'>");
						html.append("       <i class='menu-icon fa fa-caret-right'></i>");// 图标
						html.append("      <span class='menu-text'>").append(_childMenuName).append("</span>");
						html.append("</a><b class='arrow fa fa-angle-down'></b></li>");
					}
				}
				html.append("   </ul>");
				html.append("</li>");
			} else {
				html.append("<li class='").append(liclass).append("'>");
				html.append("<a target='main_frame' href='javascript:void(0)'>");
				html.append("<i class='menu-icon fa fa-tachometer'></i>");// 图标
				html.append("	<span class='menu-text'>").append(topMenuName).append("</span>");
				html.append("</a><b class='arrow'></b>");
				html.append("</li>");
			}
		}
		return html.toString();
	}

	/**
	 * 是否有子菜单
	 * 
	 * @param menuId
	 *            菜单ID
	 * @param rootMenuSubsMap
	 *            菜单ID与二级菜单对应MAP
	 * @return
	 */
	public static boolean hasChild(String menuId, TreeMap<String, List<MenuVO>> rootMenuSubsMap) {
		boolean hasChild = false;
		if (rootMenuSubsMap == null || rootMenuSubsMap.size() == 0) {
			return false;
		}
		if (rootMenuSubsMap.containsKey(menuId)) {
			hasChild = true;
		}
		return hasChild;
	}

	public static boolean _hasChild(String menuId, String childMenuId) {
		boolean _hasChild = false;
		if (Integer.valueOf(menuId) == Integer.valueOf(childMenuId)) {
			_hasChild = true;
		}
		return _hasChild;
	}

	public static Boolean getIsLRlayout() {
		return isLRlayout;
	}

	public static void setIsLRlayout(Boolean isLRlayout) {
		MenuTreeBuilder.isLRlayout = isLRlayout;
	}

	/**
	 * 根据最层数循环加载菜单
	 * 
	 * @param menuSubsMaps
	 *            数据源
	 * @param contentPath
	 *            上下文对象
	 * @return
	 */
	public static String buildTree2(List<TreeMap<String, List<MenuVO>>> menuSubsMaps, String contentPath) {
		String liclass = (isLRlayout == true) ? "" : "hover";
		StringBuffer html = addMenu(new StringBuffer(""), menuSubsMaps, liclass, 0, new MenuVO(), contentPath);
		return html.toString();
	}

	@SuppressWarnings("unchecked")
	public static StringBuffer addMenu(StringBuffer html, List<TreeMap<String, List<MenuVO>>> menuSubsMaps,
			String liclass, int i, MenuVO menu, String contentPath) {
		if (i >= menuSubsMaps.size()) {
			return html;
		}
		List<MenuVO> list = new ArrayList<>();
		TreeMap<String, List<MenuVO>> treeMap = menuSubsMaps.get(i);// 当前级别的所有记录
		if (i == 0) {
			list = treeMap.get(Constants.ROOT_MENU_ID);
		} else {
			list = treeMap.get(menu.getControlid());
		}

		for (MenuVO menuVO : list) {
			String pid = menu.getControlid() == null ? Constants.ROOT_MENU_ID : menu.getControlid();
			if (pid.equals(menuVO.getPcontrolid())) {
				String MenuName = menuVO.getLname();
				String MenuId = menuVO.getControlid();
				String MenuIconClass = menuVO.getIconclass();
				if (StringUtils.isBlank(MenuIconClass)) {
					MenuIconClass = "menu-icon fa fa-list";
				}
				html.append("<li class='").append(liclass).append("'>");
				if (StringUtils.isEmpty(menuVO.getUrl())) {
					html.append("	<a class='dropdown-toggle'>");
					html.append("       <i class='").append(MenuIconClass).append("'></i>");
				} else {
					html.append("<a class='ioscut' target='main_frame' href='").append(contentPath + menuVO.getUrl())
							.append("'>");
					html.append("       <i class='menu-icon fa fa-caret-right'></i>");
				}
				html.append("		<span class='menu-text'>").append(MenuName).append("</span>");
				if (pid == "00") {
					html.append("   <b class='arrow fa fa-angle-down'></b>");
				} else {
					html.append("   <b class='arrow'></b>");
				}
				html.append("   </a>	<ul class='submenu'>");
				int num = i;
				addMenu(html, menuSubsMaps, liclass, ++i, menuVO, contentPath);
				i = num;
				html.append("   </ul></li>");

			}

		}
		return html;
	}

}
